<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 文章管理
 */
class Article extends Auth_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('article_mdl');
    }
    /**
     * 文章列表
     */
    public function index(){

        /** 查询条件组装 */
        $url = '';
        $this->db->start_cache();
        $this->db->where('status !=',2);

        $keywords = $this->input->get('keywords');
        if(!empty($keywords)){
            $this->db->like('title', $keywords);
            $url = 'keywords='.$keywords.'&';
        }
        $sid = $this->input->get('search-sort');
        if(!empty($sid)){
            $this->db->where('sort_id',$sid);
            $url .= 'search-sort='.$sid.'&';
        }
        $this->db->stop_cache();

        $this->load->library('pagination');

        if(!empty($url)){
            $url = '?'.substr($url, 0 , -1);
        }

        $config['base_url'] = site_url('article/index').$url;
        $config['total_rows'] = $this->article_mdl->get_count();
        $config['per_page'] = 10;
        $config['page_query_string'] = TRUE;
        $config['first_link'] = '首页';
        $config['last_link'] = '尾页';
        $config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['anchor_class'] = 'current';

        $this->pagination->initialize($config);
        $data['page'] = $this->pagination->create_links();

        $per_page = $this->input->get('per_page');
        $this->db->limit($config['per_page'],intval($per_page));
        $this->db->select('id,title,set_time,user_id,click,status,sort_id');
        $this->db->order_by('id desc');
        $list = $this->article_mdl->select();
        $this->db->flush_cache();

        $this->load->model('users_mdl');
        $this->load->model('sort_mdl');
        $list_sort = $this->sort_mdl->select();
        foreach($list as $k => $v){
            $this->db->select('username');
            $this->db->where('id',$v['user_id']);
            $user = $this->users_mdl->get_one();
            $list[$k]['username'] = $user['username'];

            foreach($list_sort as $key => $value){
                if($value['id'] == $v['sort_id']){
                    $list[$k]['_sort'] = $value;
                }
            }
            if(empty($list[$k]['_sort'])){
                $list[$k]['_sort'] = '-- 未分类 --';
            }
        }
        $data['list'] = $list;
        $data['list_sort'] = $list_sort;
        $this->_view('index',$data);
    }

    /**
     * 文档选择
     */
    public function choice(){
        if($_SESSION['user']['status'] == 3){
            show_error('您没有权限打开该页面');
        }

        if($this->input->is_ajax_request()){
            $keywords = $this->input->post('author');

            $this->db->where('id',intval($keywords));
            $this->db->select('id,title,set_time,click');
            $this->db->or_like('title',$keywords);
            $this->db->where('status',1);
            $this->db->limit(10);
            $this->db->order_by('id desc ');
            $list = $this->article_mdl->select();

            foreach($list as $k => $v){
                $list[$k]['date'] = date('Y-m-d H:i:s',$v['set_time']);
            }

            echo json_encode($list);
        }else{
            $this->load->view('default/article/choice');
        }
    }

    /**
     * 文章显示
     */
    public function show(){
        $aid = $this->input->get('aid');
        $aid = intval($aid);

        if(empty($aid)){
            show_error('参数错误！');
        }

        $this->db->where('id',$aid);
        $res = $this->article_mdl->get_one();

        if(empty($res)){
            show_error('参数错误！');
        }

        $this->db->set('click','click+1',FALSE);
        $this->db->where('id',$aid);
        $this->article_mdl->update();
        $this->load->view('article_show',array('res'=>$res));
    }

    /**
     * 删除文章
     */
    public function delete(){
        if(!$this->input->is_ajax_request()){
            show_error('参数错误！');
        }
        $aid = $this->input->post('aid');
        $aid = intval($aid);

        $this->load->model('project_mdl');
        $this->db->where('article_id',$aid);
        $c = $this->project_mdl->get_one();
        if($c){
            die('该文档在“'.$c['title'].'”项目中正在使用，不能删除');
        }

        $this->db->where('id',$aid);
        $res = $this->article_mdl->delete();
        echo $res === false ? '删除失败！' : 1 ;
    }

    /**
     * 新增文章
     */
    public function create(){
        $this->db->where('status',2);
        $this->db->select('id,title');
        $res = $this->article_mdl->get_one();
        if(!empty($res['id'])){
            redirect(site_url('article/update?aid='.$res['id']));
        }else{
            $data = array(
                'title' => '请输入标题',
                'content' => '请输入内容',
                'set_time' => time(),
                'user_id' => $_SESSION['user']['id'],
                'click' => 0,
                'status' => 2
            );

            $add_id = $this->article_mdl->create($data);
            redirect(site_url('article/update?aid='.$add_id));
        }
    }

    /**
     * 修改文章
     */
    public function update(){
        $aid = $this->input->get('aid');
        $aid = intval($aid);

        if(empty($aid)){
            show_error('参数错误！');
        }

        $this->db->where('id',$aid);
        $res = $this->article_mdl->get_one();

        $this->load->model('sort_mdl');
        $list_sort = $this->sort_mdl->select();

        $this->_view('update',array('res'=>$res,'list_sort'=>$list_sort));
    }

    /**
     * 文章修改
     */
    public function article_update(){
        $post = $this->input->post(NULL);

        if(empty($post['aid'])){
            die('参数错误！');
        }

        $data = array(
            'title' => $post['title'],
            'content' => $this->input->post('content',FALSE),
            'set_time' => time(),
            'status' => 1,
            'sort_id' => $post['sort_id']
        );
        $this->db->where('id',$post['aid']);
        $res = $this->article_mdl->update($data);

        if($res === false){
            show_error('操作失败！');
        }else{
            $this->session->set_userdata('mass','操作成功！');
            redirect(site_url('article/update?aid='.$post['aid']));
        }
    }

}

/* End of file Article.php */
/* Location: ./application/controllers/Article.php */